;;定义tally对象
(defclass tally()
  ((id :reader id
       :initarg :id)
   (create-time :accessor create-time 
         :initarg :create-time)
   (description :accessor description
             :initarg :description)
   (debit-account :accessor debit-account
        :initarg :debit-account)
   (credit-account :accessor credit-account 
        :initarg :credit-account)
   (amount :accessor amount 
        :initarg :amount)
   )
  )
;;初始化系统
(defvar *p-system* (cl-prevalence:make-prevalence-system #p"./p-system/"))
;;创建计数器
(or (> (length (cl-prevalence:find-all-objects *p-system* 'tally)) 0)
    (cl-prevalence:tx-create-id-counter *p-system*))
(hunchentoot:start (make-instance 'hunchentoot:easy-acceptor :port 4242))
;;创建 people 对象至流行持久化系统
(defun make-tally (create-time description debit-account credit-account amount)
  (cl-prevalence:tx-create-object
   *p-system*
   'tally
   `(
     (create-time , create-time)
     (description , description)
     (debit-account , debit-account)
     (credit-account , credit-account)
     (amount, amount)
))
  (cl-prevalence:snapshot *p-system*) 
  (cl-prevalence:find-all-objects *p-system* 'tally)
)
;;根据参数构建一个 people 对象，转换成 JSON 然后返回
(hunchentoot:define-easy-handler (say-you :uri "/tally") (create-time description debit-account credit-account amount)
  (setf (hunchentoot:content-type*) "application/json")
  (json:encode-json-to-string
   (make-tally create-time description debit-account credit-account amount)
))
;;获取流行持久化系统内所有的 people 实例
(hunchentoot:define-easy-handler (people :uri "/tally/list") ()
  (setf (hunchentoot:content-type*) "application/json")
  (json:encode-json-to-string
   (cl-prevalence:find-all-objects *p-system* 'tally)))
